﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace c_Windows
{
    public partial class FileView : Form
    {
        TreeNode subnode;
        public FileView()
        {
            InitializeComponent();
        }
        private void FileView_Load(object sender, EventArgs e)
         {
             
         }
         private void expendtree(string path,TreeNode tn)
         {
             try
             {
                 ///获取父节点目录的子目录
                 string[] s1 = Directory.GetDirectories(path);
                 ///子节点
                 subnode = new TreeNode();
                 ///通过遍历给传进来的父节点添加子节点
                 foreach (string j in s1)
                 {
                     subnode = new TreeNode(j);
                     tn.Nodes.Add(subnode);
                     ///对文件夹不断递归， 得到所有文件
                     expendtree(j, subnode);
                 }
             }
             catch (UnauthorizedAccessException) { tn.Nodes.Add("null"); }
             catch (DirectoryNotFoundException) { MessageBox.Show("请修改路径！", "\0", MessageBoxButtons.OK, MessageBoxIcon.Error); }
         }

         private void button1_Click(object sender, EventArgs e)
         {
             try
             {
                 ///获取所有驱动盘目录
                 string[] path = {textBox1.Text.ToString()};
                 ///通过遍历去添加所有父节点
                 foreach (string m in path)
                 {
                     ///父节点
                     TreeNode node = new TreeNode(m);
                     ///给treeview添加节点
                     this.treeView1.Nodes.Add(node);
                     ///调用方法递归出磁盘的所有文件，并将父节点和路径传入
                     expendtree(m, node);
                 }
             }
             catch { }
         }
    }
}
